//
//  SuggestionViewController.m
//  DTSampleSocket
//
//  Created by ZhangYeKun on 2017/3/8.
//  Copyright © 2017年 Fidetro. All rights reserved.
//

#import "SuggestionViewController.h"
#import "FIDPlaceholderTextView.h"
#import "NSString+FIDHelper.h"
#import "PublicFeedbackApi.h"
@interface SuggestionViewController ()
@property (weak, nonatomic) IBOutlet UITextField *contactTextField;
@property (weak, nonatomic) IBOutlet FIDPlaceholderTextView *suggestTextView;
@property (weak, nonatomic) IBOutlet UILabel *wordNumLabel;

@end

@implementation SuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationStyle];
    [self.suggestTextView addObserver:self forKeyPath:@"textLength" options:NSKeyValueObservingOptionNew||NSKeyValueChangeOldKey context:nil];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    
}

- (void)onLeftButtonClick:(id)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (IBAction)tureBtn:(id)sender {
    
    [self onRightButtonClick:nil];
}

- (void)onRightButtonClick:(id)sender{
    NSString *contact = self.contactTextField.text;
    NSString *suggest = self.suggestTextView.text;
    if ([contact isEqualToString:@"请描述您的问题或意见，我们会第一时间联系您。"]) {
        [SVProgressHUD showErrorWithStatus:@"建议不能为空噢"];
        return;
    }
    if (![suggest isChineseCharacter]) {
        [SVProgressHUD showErrorWithStatus:@"建议只能输入中文、字母或数字"];
        return;
    }
    
    if (![contact isMobileNumber]) {
        if (![contact isQQNumberCharacter]) {
            if (![contact isValidateEmail]) {
                [SVProgressHUD showErrorWithStatus:@"请留下正确的电话、QQ、邮箱"];
                return;
            }
        }
    }


    
    NSDictionary *parameter =[contact length]>0? @{@"content":self.suggestTextView.text,@"contact_way":contact,@"type_id":DTCloudKitAppId}:@{@"content":suggest,@"type_id":DTCloudKitAppId};
    PublicFeedbackApi * feebackApi = [[PublicFeedbackApi alloc]initWithDic:parameter];
    
    [self startWithYTKRequest:feebackApi completionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        [SVProgressHUD showSuccessWithStatus:[request responseJSONObject][@"errmsg"]];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:nil];


    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if (change[NSKeyValueChangeNewKey] != change[NSKeyValueChangeOldKey]) {
        self.wordNumLabel.text = [NSString stringWithFormat:@"%@/400",change[NSKeyValueChangeNewKey]];
    }
}



- (void)setNavigationStyle{
    
    [self setNavigationTitle:@"意见反馈" titleColor:nil];

    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    
}

- (void)dealloc
{
    [self.suggestTextView removeObserver:self forKeyPath:@"textLength"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
